// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	12/12/2014 9:28:56 PM				
// -----------------------------

#include "Mutex.h"

Mutex::Mutex()
	: m_hMutex(NULL)
{
}

Mutex::~Mutex()
{
	if (m_hMutex)
		CloseHandle(m_hMutex);
}

Bool Mutex::Init(const TChar* lpName)
{
	m_hMutex = CreateMutex(NULL, FALSE, lpName);
	if (m_hMutex == NULL)
		return false;
	return true;
}
Bool Mutex::TryEnter(Int32 TimeOut) const
{
	DWORD dwTimeOut = (DWORD)TimeOut;
	if (TimeOut < 0)
		dwTimeOut = INFINITE;
	DWORD dwWaitStatus = WaitForSingleObject(m_hMutex, dwTimeOut);
	if (dwWaitStatus == WAIT_OBJECT_0)
		return true;
	return false;
}
Bool Mutex::Leave() const
{
	BOOL res = ReleaseMutex(m_hMutex);
	if (res != 0)
		return true;
	return false;
}